package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.adapter.GYImageAdapter;
import com.yunwaikeji.gy_tool_library.adapter.recycler.GYRecyclerAdapter;
import com.yunwaikeji.gy_tool_library.adapter.recycler.OnItemChildClickListener;
import com.yunwaikeji.gy_tool_library.adapter.recycler.OnItemClickListener;
import com.yunwaikeji.gy_tool_library.model.GYImageListModel;
import com.yunwaikeji.gy_tool_library.info.GYImageListViewInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.utils.GYUtils;
import com.yunwaikeji.gy_tool_library.utils.GYRecyclerViewUtils;
import com.yunwaikeji.gy_tool_library.view.GYLinearLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYImageListView extends GYLinearLayout implements GYImageListViewInfo, OnItemClickListener, OnItemChildClickListener {
    public GYImageListView(Context context) {
        super(context);
        init(null);
    }

    public GYImageListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public GYImageListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public GYImageListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private GYImageAdapter gyImageAdapter = new GYImageAdapter();
    private List<GYImageListModel> gyImageListBeanList = new ArrayList<>();
    private RecyclerView imageListRecyclerView;

    private boolean isEdit = false;
    private int maxSpanCount = 4;
    private int maxImageNumber = 999999999;
    private int spacing = 0;
    private int spacingColor = Color.TRANSPARENT;
    private int spacingVertical;
    private int spacingVerticalColor;
    private int spacingHorizontal;
    private int spacingHorizontalColor;

    private Drawable addDrawable;
    private Drawable deleteDrawable;
    private int deleteDrawableWidthPx = WRAP_CONTENT;
    private int deleteDrawableHeightPx = WRAP_CONTENT;
    private int listBackgroundColor = Color.TRANSPARENT;

    private void init(AttributeSet attributeSet) {
        int itemHeightPx = WRAP_CONTENT;
        String itemProportion = null;
        int deleteDrawableMarginTopPx = 0;
        int deleteDrawableMarginRightPx = 0;
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYImageListView);
            addDrawable = typedArray.getDrawable(R.styleable.GYImageListView_addDrawable);
            deleteDrawable = typedArray.getDrawable(R.styleable.GYImageListView_deleteDrawable);
            deleteDrawableWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_deleteDrawableWidth, deleteDrawableWidthPx);
            deleteDrawableHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_deleteDrawableHeight, deleteDrawableHeightPx);
            deleteDrawableMarginTopPx = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_deleteDrawableMarginTop, deleteDrawableMarginTopPx);
            deleteDrawableMarginRightPx = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_deleteDrawableMarginRight, deleteDrawableMarginRightPx);
            isEdit = typedArray.getBoolean(R.styleable.GYImageListView_isEdit, isEdit);
            maxSpanCount = typedArray.getInteger(R.styleable.GYImageListView_maxSpanCount, maxSpanCount);
            maxImageNumber = typedArray.getInteger(R.styleable.GYImageListView_maxImageNumber, maxImageNumber);
            spacing = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_spacing, spacing);
            spacingColor = typedArray.getColor(R.styleable.GYImageListView_spacingColor, spacingColor);
            spacingHorizontal = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_spacingHorizontal, spacing);
            spacingHorizontalColor = typedArray.getColor(R.styleable.GYImageListView_spacingHorizontalColor, spacingColor);
            spacingVertical = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_spacingVertical, spacing);
            spacingVerticalColor = typedArray.getColor(R.styleable.GYImageListView_spacingVerticalColor, spacingColor);
            listBackgroundColor = typedArray.getColor(R.styleable.GYImageListView_listBackgroundColor, listBackgroundColor);
            itemHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYImageListView_itemHeight, itemHeightPx);
            itemProportion = typedArray.getString(R.styleable.GYImageListView_itemProportion);
            typedArray.recycle();
        }
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_gy_image_list, null);
        addView(view);
        imageListRecyclerView = view.findViewById(R.id.imageListRecyclerView);
        gyImageAdapter.setData(gyImageListBeanList);
        GYRecyclerViewUtils.getInstance().gridLayout(imageListRecyclerView)
                .setSpanCount(maxSpanCount)
                .setDividerVerticalPx(spacingVertical)
                .setDividerVerticalColor(spacingVerticalColor)
                .setDividerHorizontalPx(spacingHorizontal)
                .setDividerHorizontalColor(spacingHorizontalColor)
                .build();
        imageListRecyclerView.setBackgroundColor(spacingColor);
        imageListRecyclerView.setAdapter(gyImageAdapter);
        setAddDrawable(addDrawable == null ? getResources().getDrawable(R.drawable.gylibrary_add) : addDrawable);
        setDeleteDrawable(deleteDrawable == null ? getResources().getDrawable(R.drawable.gylibrary_delete_circle) : deleteDrawable);
        setDeleteDrawableWidthPx(deleteDrawableWidthPx);
        setDeleteDrawableHeightPx(deleteDrawableHeightPx);
        setItemHeightPx(itemHeightPx);
        setItemProportion(itemProportion);
        setDeleteDrawableMarginTopPx(deleteDrawableMarginTopPx);
        setDeleteDrawableMarginRightPx(deleteDrawableMarginRightPx);
        gyImageAdapter.setOnItemChildClickListener(this);
        gyImageAdapter.setOnItemClickListener(this);
    }

    public GYImageListView setDeleteDrawableMarginTopPx(int deleteDrawableMarginTopPx) {
        gyImageAdapter.setDeleteDrawableMarginTopPx(deleteDrawableMarginTopPx);
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setDeleteDrawableMarginRightPx(int deleteDrawableMarginRightPx) {
        gyImageAdapter.setDeleteDrawableMarginRightPx(deleteDrawableMarginRightPx);
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setListBackgroundColor(int listBackgroundColor) {
        this.listBackgroundColor = listBackgroundColor;
        imageListRecyclerView.setBackgroundColor(listBackgroundColor);
        return this;
    }

    public GYImageListView setItemHeightPx(int itemHeightPx) {
        gyImageAdapter.setItemHeightPx(itemHeightPx);
        notifyDataSetChanged();
        return this;
    }

    public int getItemHeightPx() {
        return gyImageAdapter.getItemHeightPx();
    }

    public GYImageListView setItemHeightDp(int itemHeightDp) {
        return setItemHeightPx(GYDisplayUtils.dp2px(getContext(), itemHeightDp));
    }

    public GYImageListView setItemProportion(String itemProportion) {
        gyImageAdapter.setItemProportion(itemProportion);
        return this;
    }

    public GYImageListView setAddDrawable(Drawable addDrawable) {
        gyImageAdapter.setAddDrawable(addDrawable);
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setDeleteDrawable(Drawable deleteDrawable) {
        gyImageAdapter.setDeleteDrawable(deleteDrawable);
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setDeleteDrawableWidthDp(int deleteDrawableWidthDp) {
        return setDeleteDrawableWidthPx(GYDisplayUtils.dp2px(getContext(), deleteDrawableWidthDp));
    }

    public GYImageListView setDeleteDrawableWidthPx(int deleteDrawableWidthPx) {
        return setDeleteDrawableSizePx(deleteDrawableWidthPx, deleteDrawableHeightPx);
    }

    public GYImageListView setDeleteDrawableHeightDp(int deleteDrawableHeightDp) {
        return setDeleteDrawableHeightPx(GYDisplayUtils.dp2px(getContext(), deleteDrawableHeightDp));
    }

    public GYImageListView setDeleteDrawableHeightPx(int deleteDrawableHeightPx) {
        return setDeleteDrawableSizePx(deleteDrawableWidthPx, deleteDrawableHeightPx);
    }

    public GYImageListView setDeleteDrawableSizePx(int deleteDrawableWidthPx, int deleteDrawableHeightPx) {
        this.deleteDrawableWidthPx = deleteDrawableWidthPx;
        this.deleteDrawableHeightPx = deleteDrawableHeightPx;
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setDeleteDrawableSizeDp(int deleteDrawableWidthDp, int deleteDrawableHeightDp) {
        return setDeleteDrawableSizePx(GYDisplayUtils.dp2px(getContext(), deleteDrawableWidthDp), GYDisplayUtils.dp2px(getContext(), deleteDrawableHeightDp));
    }

    public List<GYImageListModel> getImages() {
        return gyImageListBeanList.get(gyImageListBeanList.size() - 1).getPathType() == -1 ? gyImageListBeanList.subList(0, gyImageListBeanList.size() - 1) : gyImageListBeanList;
    }

    public GYImageListView setMaxImageNumber(int maxImageNumber) {
        this.maxImageNumber = maxImageNumber;
        return this;
    }

    public GYImageListView setEditAll(boolean isEdit) {
        GYImageListView.this.isEdit = isEdit;
        for (GYImageListModel gyImageListBean : gyImageListBeanList) {
            if (gyImageListBean.getPathType() != -1) {
                gyImageListBean.setEdit(isEdit);
            }
        }
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setEditPosition(boolean isEdit, List<Integer> positionList) {
        for (Integer integer : positionList) {
            if (integer < gyImageListBeanList.size() - 1) {
                gyImageListBeanList.get(integer).setEdit(isEdit);
                gyImageAdapter.notifyItemChanged(integer);
            }
        }
        return this;
    }

    private boolean showAdd = false;

    public GYImageListView setShowAdd(boolean showAdd) {
        this.showAdd = showAdd;
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView setImagePaths(String... strings) {
        setImagePaths(Arrays.asList(strings));
        return this;
    }

    public GYImageListView setImagePaths(List<String> stringList) {
        setImagePaths(isEdit, stringList);
        return this;
    }

    public GYImageListView setImagePaths(boolean isEdit, String... strings) {
        setImagePaths(isEdit, Arrays.asList(strings));
        return this;
    }

    public GYImageListView setImagePaths(boolean isEdit, List<String> stringList) {
        GYImageListView.this.gyImageListBeanList.clear();
        addImagePaths(isEdit, stringList);
        return this;
    }

    public GYImageListView setImageBeans(GYImageListModel... gyImageListBeans) {
        setImageBeans(Arrays.asList(gyImageListBeans));
        return this;
    }

    public GYImageListView setImageBeans(List<GYImageListModel> gyImageListBeanList) {
        GYImageListView.this.gyImageListBeanList.clear();
        addImageBeans(gyImageListBeanList);
        return this;
    }

    public GYImageListView addImagePaths(String... strings) {
        addImagePaths(isEdit, Arrays.asList(strings));
        return this;
    }

    public GYImageListView addImagePaths(boolean isEdit, String... strings) {
        addImagePaths(isEdit, Arrays.asList(strings));
        return this;
    }

    public GYImageListView addImagePaths(List<String> stringList) {
        addImagePaths(isEdit, stringList);
        return this;
    }

    public GYImageListView addImagePaths(boolean isEdit, List<String> stringList) {
        for (String path : stringList) {
            GYImageListModel gyImageListBean = new GYImageListModel();
            gyImageListBean.setPath(path);
            gyImageListBean.setEdit(isEdit);
            int pathType;
            if (GYUtils.isNumber(path)) {
                pathType = PathType.RESOURCE;
            } else if (TextUtils.isEmpty(path) || (path.length() >= 4 && path.substring(0, 4).equals("http"))) {
                pathType = PathType.NETWORK;
            } else {
                pathType = PathType.LOCAL;
            }
            gyImageListBean.setPathType(pathType);
            addImageBeans(gyImageListBean);
        }
        return this;
    }

    public GYImageListView addImageBeans(GYImageListModel... gyImageListBeans) {
        addImageBeans(Arrays.asList(gyImageListBeans));
        return this;
    }

    public GYImageListView addImageBeans(List<GYImageListModel> gyImageListBeanList) {
        GYImageListView.this.gyImageListBeanList.addAll(gyImageListBeanList);
        notifyDataSetChanged();
        return this;
    }

    public GYImageListView addImageRes(boolean isEdit, Integer... integers) {
        return addImageRes(isEdit, Arrays.asList(integers));
    }

    public GYImageListView addImageRes(boolean isEdit, List<Integer> integerList) {
        List<String> stringList = new ArrayList<>();
        for (Integer integer : integerList) {
            stringList.add(String.valueOf(integer));
        }
        return addImagePaths(isEdit, stringList);
    }

    public GYImageListView addImageRes(Integer... integers) {
        return addImageRes(Arrays.asList(integers));
    }

    public GYImageListView addImageRes(List<Integer> integerList) {
        return addImageRes(isEdit, integerList);
    }

    private GYImageListModel getAddBean() {
        GYImageListModel gyImageListBean = new GYImageListModel();
        gyImageListBean.setPath(String.valueOf(addDrawable));
        gyImageListBean.setEdit(false);
        gyImageListBean.setPathType(-1);
        return gyImageListBean;
    }

    private void removeAddBean() {
        for (int i = gyImageListBeanList.size() - 1; i >= 0; i--) {
            if (gyImageListBeanList.get(i).getPathType() == -1) {
                gyImageListBeanList.remove(i);
            }
        }
    }

    public void notifyDataSetChanged() {
//        initAddDelete();
        removeAddBean();
        for (int i = gyImageListBeanList.size() - 1; i >= 0; i--) {
            if (gyImageListBeanList.size() > maxImageNumber) {
                gyImageListBeanList.remove(i);
            }
        }
        if ((gyImageListBeanList.size() < maxImageNumber && isEdit) || showAdd) {
            gyImageListBeanList.add(getAddBean());
        }
        gyImageAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemChildClick(GYRecyclerAdapter<?,?> adapter, View view, int position) {
        if (view.getId() == R.id.delImg) {
            if (onDeleteClickListener != null) {
                if (!onDeleteClickListener.onDeleteClick(position)) {
                    gyImageListBeanList.remove(position);
                    notifyDataSetChanged();
                }
            } else {
                gyImageListBeanList.remove(position);
                notifyDataSetChanged();
            }
        }
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public GYImageListView setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    private OnAddClickListener onAddClickListener;

    public interface OnAddClickListener {
        void onAddClick();
    }

    public GYImageListView setOnAddClickListener(OnAddClickListener onAddClickListener) {
        this.onAddClickListener = onAddClickListener;
        return this;
    }

    private OnDeleteClickListener onDeleteClickListener;

    public interface OnDeleteClickListener {
        boolean onDeleteClick(int position);
    }

    public GYImageListView setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
        return this;
    }

    public GYImageListView remove(int position) {
        if (position >= 0 && position < gyImageListBeanList.size()) {
            if (gyImageListBeanList.get(position).getPathType() >= 0) {
                gyImageListBeanList.remove(position);
                gyImageAdapter.notifyDataSetChanged();
            }
        }
        return this;
    }

    @Override
    public void onItemClick(GYRecyclerAdapter<?,?> adapter, View view, int position) {
        GYImageListModel gyImageListBean = gyImageListBeanList.get(position);
        if (gyImageListBean.getPathType() == -1) {
            if (onAddClickListener != null) {
                onAddClickListener.onAddClick();
            }
        } else {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(position);
            }
        }
    }
}
